/*******************************************************************************
 * Package: com.song.kkxxpoi.controller
 * Type:    ArthasWeb
 * Date:    2025/5/23 10:31
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.kkxxpoi.controller;

import com.song.common.annotation.ResponseInfoSkin;
import com.song.kkxxpoi.entity.IntEndScaleForecastEntity;
import com.song.kkxxpoi.mapper.IIntEndScaleForecastMapper;
import com.song.kkxxpoi.service.ArthasService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 功能描述：
 *
 * @author Songxianyang
 * @date 2025/5/23 10:31
 */
@Slf4j
@Api(tags = "Arthas-使用")
@RestController
@RequiredArgsConstructor
@RequestMapping("/arthas-web")
@ResponseInfoSkin
public class ArthasWeb {

    private final ArthasService arthasService;
    private final IIntEndScaleForecastMapper iIntEndScaleForecastMapper;
    @SneakyThrows
    @GetMapping(value = "/index")
    @ApiOperation("接口的入参出参")
    public int index(int p1,int p2) {

        log.info("我是一条info日志");
        TimeUnit.SECONDS.sleep(5);

        return p1+p2;
    }


    @SneakyThrows
    @GetMapping(value = "/throwExpMethodArthas")
    @ApiOperation("有异常的方法-Arthas")
    public int throwExpMethodArthas(int p1,int p2) {
        log.info("异常的方法");
        int a = 1/0;
        return p1+p2;
    }

    // 查看方法的总耗时
    @SneakyThrows
    @GetMapping(value = "/viewTheTotalTimeSpentOnTheMethod")
    @ApiOperation("查看方法的总耗时")
    public int viewTheTotalTimeSpentOnTheMethod(int p1,int p2) {
        TimeUnit.SECONDS.sleep(5);
        return p1+p2;
    }

    // 查看方法的调用链路
    @SneakyThrows
    @GetMapping(value = "/viewTheInvocationOfTheMethod")
    @ApiOperation("查看方法的调用链路")
    public void viewTheInvocationOfTheMethod() {
        TimeUnit.SECONDS.sleep(2);
        arthasService.m1();
        arthasService.m2();
    }

    @RequestMapping(value="/find", method={RequestMethod.GET}, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @ApiOperation(httpMethod = "GET", value = "查询sql打印")
    public List<IntEndScaleForecastEntity> find() {
        return iIntEndScaleForecastMapper.listAll();
    }

    // 查看系统cpu利用率最大的是哪个线程
    @SneakyThrows
    @GetMapping(value = "/cpu")
    @ApiOperation("查看系统cpu利用率最大的是哪个线程")
    public void cpu() {
        arthasService.t1();
    }


    /**
     -Xmx100m
     -Xms100m
     -XX:+HeapDumpOnOutOfMemoryError
     -XX:HeapDumpPath=D:\java-project\dump
     -XX:+PrintGC

     D:\work\project\dump

     HeapDumpOnOutOfMemoryError：OOM的时候会自动产生堆转储文件
     */
    @GetMapping("/outOfMemoryErrorTest")
    @ApiOperation(" OOM问题排查")
    public void outOfMemoryErrorTest() {
        List<TestWrapper> memoryList = new ArrayList<>();
        while (true){
            byte[] b = new byte[1024];
            memoryList.add(new TestWrapper(b));
            // memoryList.clear();
        }
    }



    @GetMapping("/max-http-header-size")
    @ApiOperation(" OOM之max-http-header-size")
    public void maxHttpHeaderSize(HttpServletResponse response) {
        System.out.println("oom");
        System.out.println("oom");
    }










}




class TestWrapper{
    byte[] b;

    public TestWrapper(byte[] b) {
        this.b = b;
    }
}
